﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CustomControls;
using Model;
using DataAccess;
using Model.EntityModel;
using System.Collections.ObjectModel;

namespace Project
{
    /// <summary>
    /// Lógica de interacción para RegisterAudienceContent.xaml
    /// </summary>
    public partial class RegisterAudienceContent : UserControl
    {
        public AudienceClass audience;
        public SearchComplaintWindow sc;
        public long idComplaint;
        public Model.EntityModel.Actor Denouncer;
        public Model.EntityModel.Actor Agressor;
        bool modify = false;
        
        public RegisterAudienceContent()
        {
            InitializeComponent();
            UIWindowManager.SetWindowTitle(this, "Brigada De Protección a la Familia - RegisterAudience");
            lbDate.Content = DateTime.Now.Date.ToShortDateString();
            lbTime.Content = DateTime.Now.ToShortTimeString();
            audience = new AudienceClass();
            audience.Id = DataBaseAccess.NumberAudiences;
            audience.Status = false;
        }

        public RegisterAudienceContent(AudienceClass aud)
        {
            InitializeComponent();
            UIWindowManager.SetWindowTitle(this, "Brigada De Protección a la Familia - RegisterAudience");
            idComplaint = aud.IdComplaint;
            lbDate.Content = aud.Date.ToShortDateString();
            lbTime.Content = aud.Time;
            string[] time = aud.Time.Split(':');
            Charge();
            audience = new AudienceClass();
            audience = aud;
            audience.Status = true;
            //audience.CiEmployee = aud.CiEmployee;
            tbReport.Text = aud.Report;
            modify = true;
        }
        /*
        public void fillCBs()
        {
            for (int i = 8; i < 12; i++)
            {
                cbHours.Items.Add(i.ToString());
            }
            for (int i = 14; i < 18; i++)
            {
                cbHours.Items.Add(i.ToString());
            }
            string min="";
            for (int i = 0; i < 60; i++)
            {
                if (i < 10)
                    min = min + "0";
                min = i.ToString();
                cbMin.Items.Add(min);
                min = "";
            }
        }*/

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (lbCase.Content.ToString() != " " && tbReport.Text.Length > 15)
            {
                audience.IdComplaint = idComplaint;
                audience.Report = tbReport.Text;
                //audience.CiEmployee = UserControllerAndDataAccess.CurrentLoggedUser.CiEmployee;
                if(!modify)
                {
                    audience.Date = DateTime.Now.Date;
                    audience.Time = DateTime.Now.ToShortTimeString();
                }
                bool resp = false;
                if (modify)
                {
                    var grantPermission = new GrantDirectorPermissionWindows();
                    if (grantPermission.ShowDialog() == true)
                    {
                        try
                        {
                            resp = DataBaseAccess.ModifyAudience(audience);
                            if (resp)
                            {
                                MessageBoxResult result = MessageBox.Show("Guardado Correctamente! Desea imprimir?", "Informacion", MessageBoxButton.YesNo, MessageBoxImage.Question);
                                if (result == MessageBoxResult.Yes)
                                {
                                    var items = new ObservableCollection<UserControl>();
                                    items.Add(new PrintAudience(audience));
                                    var a = new PrintPreviewDialogV2(items);
                                    a.ShowDialog();
                                }
                                else
                                {
                                    Xceed.Wpf.Toolkit.MessageBox.Show("Puede imprimir el reporte de Audiencia posteriormente, entrando a Ver Denuncias, click sobre la denuncia selecciona la opción Ver Audiencias seleccionando por fecha la audiencia que desea",
                                                                    "Información!", MessageBoxButton.OK, MessageBoxImage.Information);
                                }
                                UIWindowManager.CleanWindow(this);
                            }
                            else
                            {
                                MessageBox.Show("Error al guardar");
                            }
                        }
                        catch
                        {
                            MessageBox.Show("Error al guardar");
                        }
                    }
                }
                else
                {
                    try
                    {
                        resp = DataBaseAccess.RegisterAudience(audience);
                        if (resp)
                        {
                            MessageBoxResult result = MessageBox.Show("Guardado Correctamente! Desea imprimir?", "Informacion", MessageBoxButton.YesNo, MessageBoxImage.Question);
                            if (result == MessageBoxResult.Yes)
                            {
                                var items = new ObservableCollection<UserControl>();
                                items.Add(new PrintAudience(audience));
                                var a = new PrintPreviewDialogV2(items);
                                a.ShowDialog();
                            }
                            else
                            {
                                Xceed.Wpf.Toolkit.MessageBox.Show("Puede imprimir el reporte de Audiencia posteriormente, entrando a Ver Denuncias, click sobre la denuncia selecciona la opción Ver Audiencias seleccionando por fecha la audiencia que desea",
                                                                "Información!", MessageBoxButton.OK, MessageBoxImage.Information);
                            }
                            UIWindowManager.CleanWindow(this);
                        }
                        else
                        {
                            MessageBox.Show("Error al guardar");
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Error al guardar");
                    }
                }
                if (resp)
                {
                    MessageBox.Show("Guardado correctamente");
                        UIWindowManager.CleanWindow(this);
                }
            }
            else
            {
                if(tbReport.Text.Length < 16)
                    tbReport.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                MessageBox.Show("Debe llenar todos los campos marcados con asterisco y seleccionar un caso");                
            }
        }

        public void Charge()
        {
            try
            {
                lbCase.Content = idComplaint.ToString();
                Complaint comp = new Complaint();
                comp = ComplaintDataAccess.GetComplaintById(idComplaint);
                lblNatFact.Content = comp.IncidentNature;
                lblPlace.Content = comp.IncidentPlace;
                
                Denouncer = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Denunciante", idComplaint);
                lblNameDenouncer.Content = Denouncer.Person.Name;
                lblPaternDenouncer.Content = Denouncer.Person.PaternalName;
                if(!String.IsNullOrEmpty(Denouncer.Person.MaternalName))
                    lblMaternDenouncer.Content = Denouncer.Person.MaternalName;
                
                Agressor = PersonAndActorDataAccess.GetActorByTypeAndComplaintId("Agresor", idComplaint);
                lblNameDenounced.Content = Agressor.Person.Name;
                lblPaternDenounced.Content = Agressor.Person.PaternalName;
                if (!String.IsNullOrEmpty(Agressor.Person.MaternalName))
                    lblMaternDenounced.Content = Agressor.Person.MaternalName;
            }
            catch
            {
                MessageBox.Show("Error en al obtener datos de nuestra base de datos", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }


        }

        void getComplaintEventHandler(object sender, RoutedEventArgs e)
        {
            try
            {
                idComplaint = (sc.Content.Children[0] as SearchComplaint).SelectedComplaintId;

                Charge();
            }
            catch
            {
                MessageBox.Show("Ocurrio un error al obtener el empleado", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void BuscarCaso_Click(object sender, RoutedEventArgs e)
        {
            sc = new SearchComplaintWindow();
            (sc.Content.Children[0] as SearchComplaint).listViewComplaints.MouseDoubleClick += (getComplaintEventHandler);
            sc.ShowDialog();
        }
    }
}
